<?php
class MaterialPriceCheckAction extends CAction {
	public function run($materialModelId){
		$materialModel=MaterialModel::model()->findByPk($materialModelId,array(
			'with'=>array('material')
		));
		if($materialModel===null){
			throw new CHttpException(404,'Model not found');
		}

		$materialReceiptItemModel=MaterialReceiptItem::model();
		$criteria=new CDbCriteria();
		$criteria->compare($materialReceiptItemModel->getTableAlias().'.materialModelId', $materialModelId);
		$criteria->with=array('materialReceipt');
		$criteria->order='receiptDate ASC';
		$recieptItems=$materialReceiptItemModel->findAll($criteria);
		$recentPrices=array();
		foreach ($recieptItems as $recieptItem) {
			$recentPrices[]=array(
				'date'=>$recieptItem->materialReceipt->receiptDate,
				'price'=>$recieptItem->standardUnitPrice,
			);
		}
		$result=array(
			'recentPrices'=>$recentPrices,
			'materialModel'=>array(
				'id'=>$materialModel->id,
				'material'=>$materialModel->material->attributes,
				'model'=>$materialModel->model,
				'note'=>$materialModel->note,
				'referenceCode'=>$materialModel->referenceCode,
				'unit'=>$materialModel->unit,
			)
		);
		JHTML::jsonHeader();
		echo CJSON::encode($result);
	}
}